package RestrantRecommend;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RestrantRecommendServlet extends HttpServlet {

	private static final long serialVersionUID = 5632703268698750759L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setHeader("content-type", "text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("charset=utf-8");
		PrintWriter out = response.getWriter();
		String[] category = request.getParameterValues("category");
		System.out.println("category: " + category.length + category[0]);
		for (String s : category) {
			System.out.println(s);
		}
		List<String> categorys = Arrays.asList(category);
		String[] distinct = request.getParameterValues("distinct");
		List<String> distincts = Arrays.asList(distinct);
		String[] star = request.getParameterValues("star");
		List<String> stars = Arrays.asList(star);
		String[] sort = request.getParameterValues("sort");
		System.out.println(sort.length);
		ShopSearch shopsear = new ShopSearch();
		shopsear.setCategory(categorys);
		shopsear.setDistinct(distincts);
		shopsear.setStar(stars);
		shopsear.setSort(sort[sort.length - 1]);
		List<shop> shops = new TouristRestrantRecommend(shopsear).doTouristRestrantRecommed();
		System.out.println(shops.size());
		// for (shop sh : shops) {
		// System.out.println(sh.getShopname() + " " + sh.getAdress() + " " + sh.getDistrict());
		// }
		System.out.println(sort + "  " + category.length);
		request.setAttribute("shops", shops);
		request.getRequestDispatcher("foods.jsp").forward(request, response);
		out.flush();
		out.close();
	}
}
